package com.xayy.mgt.service.impl;

import cn.kawins.base.result.CheckResult;
import cn.kawins.base.utils.CollUtil;
import cn.kawins.mybatis.base.BaseServiceImpl;
import com.xayy.mgt.mapper.NodeInfoMapper;
import com.xayy.mgt.model.AssociationParameter;
import com.xayy.mgt.model.NodeParamInfo;
import com.xayy.mgt.model.NodeTreeInfo;
import com.xayy.mgt.model.event.NodeInfoEvent;
import com.xayy.mgt.model.event.ParamUpdateEvent;
import com.xayy.mgt.model.vo.HeaderVo;
import com.xayy.mgt.service.NodeInfoService;
import com.xayy.mgt.service.NodeParamInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationEventPublisher;

@Service
public class NodeParamInfoServiceImpl extends BaseServiceImpl<NodeParamInfo> implements NodeParamInfoService {

    @Autowired
    private ApplicationEventPublisher eventPublisher;
//    @Autowired
//    private AssociationParameterService parameterService;

    @Autowired
    private NodeInfoMapper nodeInfoMapper;

    @Override
    public CheckResult<List<NodeParamInfo>> getParams(Long nodeId) {
        List<NodeParamInfo> nodeParamInfos = select(NodeParamInfo.builder().nodeId(nodeId).build());
        return CheckResult.success(nodeParamInfos);
    }

    @Override
    public CheckResult<Object> addOrUpdate(NodeParamInfo nodeParamInfo) {
        NodeTreeInfo nodeTreeInfo = nodeInfoMapper.selectOne(NodeTreeInfo.builder().id(nodeParamInfo.getNodeId()).build());
        if(nodeTreeInfo == null){
            return  CheckResult.fail("暂无该节点信息");
        }
        nodeParamInfo.setSatelliteId(nodeTreeInfo.getSatelliteId());
        if (nodeParamInfo.getId() != null) {
            CheckResult<?> checkName = checkName(true, nodeParamInfo);
            if (checkName.no()) {
                return CheckResult.fail(checkName.getCheckMsg());
            }
            CheckResult<?> checkCode = checkCode(true, nodeParamInfo);
            if (checkCode.no()) {
                return CheckResult.fail(checkCode.getCheckMsg());
            }
            /*修改关联参数参数值和code*/
            AssociationParameter parameter = AssociationParameter.builder().paramCode(nodeParamInfo.getCode()).paramName(nodeParamInfo.getName()).build();
            Example example = new Example(AssociationParameter.class);
            example.createCriteria().andEqualTo("nodeId",nodeParamInfo.getNodeId());
            eventPublisher.publishEvent(new ParamUpdateEvent(
                    nodeParamInfo.getNodeId().toString(),
                    nodeParamInfo.getCode(),
                    nodeParamInfo.getName()
            ));
//            parameterService.updateByExampleSelective(parameter,example);
            updateByIdSelective(nodeParamInfo);
            return CheckResult.success("修改成功");
        } else {
            CheckResult<?> checkName = checkName(false, nodeParamInfo);
            if (checkName.no()) {
                return CheckResult.fail(checkName.getCheckMsg());
            }
            CheckResult<?> checkCode = checkCode(false, nodeParamInfo);
            if (checkCode.no()) {
                return CheckResult.fail(checkCode.getCheckMsg());
            }
            insert(nodeParamInfo);
            return CheckResult.success("新增成功");
        }
    }

    @Override
    public CheckResult<List<HeaderVo>> getNodeHeader(Long nodeId,Integer type) {
        List<HeaderVo> headerVoList = new ArrayList<>();
        List<NodeParamInfo> nodeParamInfos = select(NodeParamInfo.builder().nodeId(nodeId).build());
        HeaderVo headerVo3 = new HeaderVo();
        headerVo3.setTitle("时间");
        headerVo3.setDataIndex("telemetryTime");
        headerVo3.setEllipsis(true);
        headerVo3.setTooltip(true);
        headerVoList.add(headerVo3);
        if (CollUtil.isEmpty(nodeParamInfos))
            return CheckResult.fail("当前节点没有遥测参数无法进行评估");
        nodeParamInfos.forEach(x -> {
            HeaderVo headerVo = new HeaderVo();
            headerVo.setTitle(x.getName());
            headerVo.setDataIndex(x.getCode());
            headerVo.setEllipsis(true);
            headerVo.setTooltip(true);
            headerVoList.add(headerVo);
        });
        if (type == 1){
            HeaderVo headerVo1 = new HeaderVo();
            headerVo1.setTitle("故障名称");
            headerVo1.setDataIndex("faultName");
            headerVo1.setEllipsis(true);
            headerVo1.setTooltip(true);
            headerVoList.add(headerVo1);
            HeaderVo headerVo2 = new HeaderVo();
            headerVo2.setTitle("故障等级");
            headerVo2.setDataIndex("faultLevel");
            headerVo2.setEllipsis(true);
            headerVo2.setTooltip(true);
            headerVoList.add(headerVo2);
        }
        return CheckResult.success(headerVoList);
    }

    /**
     * 校验参数名称是否已存在
     *
     * @param isUpdate      是否更新
     * @param nodeParamInfo 入参
     * @return 结果日志
     */
    private CheckResult<?> checkName(Boolean isUpdate, NodeParamInfo nodeParamInfo) {
        Example example = new Example(NodeParamInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("nodeId", nodeParamInfo.getNodeId());
        criteria.andEqualTo("name", nodeParamInfo.getName());
        if (Boolean.TRUE.equals(isUpdate)) {
            criteria.andNotEqualTo("id", nodeParamInfo.getId());
        }
        int i = selectCountByExample(example);
        if (i > 0) {
            return CheckResult.fail("参数名称：" + nodeParamInfo.getName() + "已经存在");
        }
        return CheckResult.success();
    }

    /**
     * 校验参数code是否已存在
     *
     * @param isUpdate      是否更新
     * @param nodeParamInfo 入参
     * @return 结果日志
     */
    private CheckResult<?> checkCode(Boolean isUpdate, NodeParamInfo nodeParamInfo) {
        Example example = new Example(NodeParamInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("nodeId", nodeParamInfo.getNodeId());
        criteria.andEqualTo("code", nodeParamInfo.getCode());
        if (Boolean.TRUE.equals(isUpdate)) {
            criteria.andNotEqualTo("id", nodeParamInfo.getId());
        }
        int i = selectCountByExample(example);
        if (i > 0) {
            return CheckResult.fail("参数code：" + nodeParamInfo.getCode() + "已经存在");
        }
        return CheckResult.success();
    }
}
